package com.scut.online_black_market.controller;

import com.scut.online_black_market.dto.SuccessResponse;
import com.scut.online_black_market.entity.User;
import com.scut.online_black_market.utility.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/redis")
@Api("redis测试接口")
public class RedisTestController {

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation(value = "redisSet")
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> setKeyValue(@RequestBody User user){
        if (redisUtil.set(user.getName(),user.getAge()) == true) {
            return ResponseEntity.ok(new SuccessResponse(true,user.getName()));
        }
        else {
            return ResponseEntity.ok(new SuccessResponse(false,user.getName()));
        }
    }

    @ApiOperation(value = "redisGet")
    @RequestMapping(value = "/get/{name}",method = RequestMethod.GET)
    public ResponseEntity<User> getValue(@PathVariable String name){
        int age = (int)redisUtil.get(name);
        User user = User.builder()
                .name(name)
                .age(age)
                .build();
        return ResponseEntity.ok(user);
    }
}
